package com.bilin.coin.common.adapter;

import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.List;

/**
 * 标签布局的适配器
 *
 */

public class TabLayoutAdapter extends FragmentPagerAdapter {

    private Context mContext;
    /**
     * 标签的item布局页面
     */
    private List<Fragment> mFragmentList;
    /**
     * 标签的名称
     */
    private List<String> mTitleList;
    private int mChildCount = 0;
    public TabLayoutAdapter(Context mContext, FragmentManager fm, List<Fragment> mFragmentList, List<String> mTitleList) {
        super(fm);
        this.mContext = mContext;
        this.mFragmentList = mFragmentList;
        this.mTitleList = mTitleList;
    }

    /**
     * tablayout的标题的名称
     *
     * @param position
     * @return
     */
    @Override
    public CharSequence getPageTitle(int position) {

        return mTitleList == null ? "" : mTitleList.get(position);
    }

    /**
     * 返回当前的fragment
     *
     * @param position
     * @return
     */
    @Override
    public Fragment getItem(int position)
    {
        return mFragmentList.get(position);
    }


    /**
     * 返回总共的页面大小
     *
     * @return
     */
    @Override
    public int getCount() {
        return mFragmentList == null ? 0 : mFragmentList.size();
    }


    /**
     * 重写父类刷新方法进行刷新
     */
    @Override
    public void notifyDataSetChanged() {
        mChildCount = getCount();
        super.notifyDataSetChanged();
    }

    /**
     * 只有改变了页数刷新才能成功
     * @param object
     * @return
     */
    @Override
    public int getItemPosition(Object object)   {
        if ( mChildCount > 0) {
            mChildCount --;
            return POSITION_NONE;
        }
        return super.getItemPosition(object);
    }

}
